package com.adgroup.smartnote;

import java.util.ArrayList;
import java.util.zip.Inflater;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import JsonModel.FamiliarFriend;
import JsonModel.User;
import Net.JsonParse;
import Utilities.Configuration;
import Utilities.Utility;
import adapter.AdapterFamiliarFriend;
import adapter.AdapterFamiliarFriend.Adapter;
import adapter.AdapterSearchFriend;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.Gallery;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import animationmenu.CollapseAnimation;
import animationmenu.ExpandAnimation;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.adgroup.smartnote.LoginActivity.AsyntaskLogin;
import com.adgroup.smartnote.SmartNoteActivity.ActivitySwipeDetector;
import com.adgroup.smartnote.SmartNoteActivity.AsynLoadAvatar;
import com.androidquery.AQuery;

import database.Database;

public class SearchFriend extends SherlockActivity{

	EditText etSearch;
	ListView lvSearch;
	@SuppressWarnings("deprecation")
	Gallery galSearch;
	Button btnSearch;
	ProgressBar progressLV;
	ProgressBar progressGal;
	int pageGal = 0;
	int pageLV = 0;
	
	AdapterFamiliarFriend.Adapter adapFamiliar;
	AdapterSearchFriend.Adapter adapSearch;
	View v;
	ArrayList<FamiliarFriend> arrs = new ArrayList<FamiliarFriend>();
	String keyWord = "";
	User u;
	private DisplayMetrics metrics;
	public int panelWidth;
	private LinearLayout menuPanel;
	private FrameLayout.LayoutParams menuPanelParameters;
	public LinearLayout slidingPanel;
	private FrameLayout.LayoutParams slidingPanelParameters;
	TextView tvNickname, tvEmail, tvGender, tvBirthday, tvFriend, tvGroup;
	Button btnMake, btnClose;
	LinearLayout rootSlidngMenuMakeFriend;
	LinearLayout.LayoutParams rootSlidngMenuMakeFriendParams;
	public boolean isExpanded;
	public FamiliarFriend userReveive;
	TextView tvNoSearch;
	private ActionBar mActionBar;
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getSupportMenuInflater();
		inflater.inflate(R.menu.menusearchfriend, menu);
		return super.onCreateOptionsMenu(menu);
	}
	@Override
	public boolean onOptionsItemSelected(
			com.actionbarsherlock.view.MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menuLoginSearchFriend:
			
			break;
		case android.R.id.home:
			finish();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	
	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.makefriend);
		
		//action bar
		mActionBar = getSupportActionBar();
		mActionBar.setDisplayHomeAsUpEnabled(false);
		mActionBar.setDisplayUseLogoEnabled(true);
		mActionBar.setHomeButtonEnabled(true);
		mActionBar.setTitle(getResources().getString(R.string.buttonlogin));
		
		//config menu slide
		metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);
		
		panelWidth = (int) ((metrics.widthPixels) * 0.85);

		
		menuPanel = (LinearLayout) findViewById(R.id.menuPanelSearchFriend);
		menuPanelParameters = (FrameLayout.LayoutParams) menuPanel
				.getLayoutParams();
		menuPanelParameters.width = panelWidth;
		menuPanel.setLayoutParams(menuPanelParameters);
		slidingPanel = (LinearLayout) findViewById(R.id.slidingmenumakefreind);
		slidingPanelParameters = (FrameLayout.LayoutParams) slidingPanel
				.getLayoutParams();
		slidingPanelParameters.width = metrics.widthPixels;
		slidingPanel.setLayoutParams(slidingPanelParameters);
		
		//mapping sliding menu
		tvBirthday = (TextView) findViewById(R.id.textViewBirthdaySlideMakeFriend);
		tvNickname = (TextView) findViewById(R.id.textViewNickNameSlideMakeFriend);
		tvEmail = (TextView) findViewById(R.id.textViewEmailSlideMakeFriend);
		tvGender = (TextView) findViewById(R.id.textViewGenderSlideMakeFriend);
		tvFriend = (TextView) findViewById(R.id.textViewFriendSlideMakeFriend);
		tvGroup = (TextView) findViewById(R.id.textViewGroupSlideMakeFriend);
		btnMake = (Button) findViewById(R.id.buttonSendSlideMakeFriend);
		btnClose = (Button) findViewById(R.id.buttonCloseSlideMakeFriend);
		tvNoSearch = (TextView) findViewById(R.id.textViewSearchEmpty);
		
		rootSlidngMenuMakeFriend = (LinearLayout) findViewById(R.id.rootslidngmenumakefriend);
		rootSlidngMenuMakeFriendParams = (LayoutParams) rootSlidngMenuMakeFriend.getLayoutParams();
		rootSlidngMenuMakeFriendParams.width = metrics.widthPixels;
		rootSlidngMenuMakeFriend.setLayoutParams(rootSlidngMenuMakeFriendParams);
		
		etSearch = (EditText) findViewById(R.id.editTextSearchFriend);
		lvSearch = (ListView) findViewById(R.id.listViewSearchFriend);
		galSearch = (Gallery) findViewById(R.id.gallerySearchFriend);
		btnSearch = (Button) findViewById(R.id.buttonSearch);
		progressGal = (ProgressBar) findViewById(R.id.progressBarWaitingResemble);
		progressLV = (ProgressBar) findViewById(R.id.progressBarWaitingSearch);
		progressLV.setVisibility(View.GONE);
		
		//add footer iv
		v = LayoutInflater.from(SearchFriend.this).inflate(R.layout.footerwating, null);
		lvSearch.addFooterView(v);
		v.setVisibility(View.GONE);
		tvNoSearch.setVisibility(View.GONE);
		
		//sliding menu
		btnClose.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {				
				isExpanded = false;

				// Collapse
				new CollapseAnimation(slidingPanel, panelWidth,
						TranslateAnimation.RELATIVE_TO_SELF, 0.75f,
						TranslateAnimation.RELATIVE_TO_SELF, 0.0f, 0, 0.0f,
						0, 0.0f);
			}
		});
		btnMake.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				u = new Database(SearchFriend.this).getUser();
				if(u==null){
					new AlertDialog.Builder(SearchFriend.this)
					.setTitle(getResources().getString(R.string.buttonlogin))
					.setMessage(getResources().getString(R.string.messagemakefriendlogin))
					.setNegativeButton(getResources().getString(R.string.ok), new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							Intent i = new Intent(SearchFriend.this, LoginActivity.class);
							startActivityForResult(i, Configuration.requestMakeFriendToLogin);
						}
					})
					.setPositiveButton(SearchFriend.this.getResources().getString(R.string.cancel),
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
								}
					}).show();
				}
				else if(userReveive.getIsAccpet().equalsIgnoreCase("1") || userReveive.getIsAccpet().equalsIgnoreCase("null")){
					new AsynSendRequest().execute(new String[]{u.getUserId(),userReveive.getUserId()});
				}
				else if(userReveive.getIsAccpet().equalsIgnoreCase("0")){
					new AsynCancelRequest().execute(new String[]{u.getUserId(),userReveive.getUserId()});
				}
			}
		}); 
		//new AsynSendRequest(u)
		//close menu sliding
		menuPanel.setOnTouchListener(new ActivitySwipeDetector());
		
		//scroll lv
		lvSearch.setOnScrollListener(new EndlessScrollListener());		
		lvSearch.setClickable(true);
		lvSearch.setItemsCanFocus(true);
		lvSearch.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, final int arg2,
					long arg3) {
				Log.d("log","click");
				if (!isExpanded) {
					isExpanded = true;
					updateInforSldingMenu(arrs.get(arg2));
					// Expand
					new ExpandAnimation(slidingPanel, panelWidth,
							Animation.RELATIVE_TO_SELF, 0.0f,
							Animation.RELATIVE_TO_SELF, 0.75f, 0, 0.0f, 0, 0.0f);
				} else {
					isExpanded = false;

					// Collapse
					new CollapseAnimation(slidingPanel, panelWidth,
							TranslateAnimation.RELATIVE_TO_SELF, 0.75f,
							TranslateAnimation.RELATIVE_TO_SELF, 0.0f, 0, 0.0f,
							0, 0.0f);

				}				
			}
		});
		//load familiar friend
		new AsynLoadResemble().execute();
		galSearch.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				
			}
		});
		
		btnSearch.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				keyWord = etSearch.getText().toString();
				String userid;
				u = new Database(SearchFriend.this).getUser();
				if(u==null){
					userid = "0";
				}
				else{
					userid = u.getUserId();
				}
				new AsynSearchFriend().execute(new String[]{keyWord,userid});
			}
		});
	}
	
	//cancel make friend			
	class AsynCancelRequest extends AsyncTask<String, Void, String>
	{
		ProgressDialog progress;
		
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			progress = ProgressDialog.show(SearchFriend.this, "",
					getResources().getString(R.string.titledialogcancelmakefriend), true);
		}
		@Override
		protected String doInBackground(String... params) {				
			return new JsonParse().sendCancelMakeFriend(SearchFriend.this, params);
		}
		@Override
		protected void onPostExecute(String result) {
			progress.dismiss();
			//Utility.ShowAlert(SearchFriend.this, getResources().getString(R.string.buttonmakefriendrequested), getResources().getString(R.string.messagemakefriendrequested)+" "+userReveive.getNickName());
			new AlertDialog.Builder(SearchFriend.this)
			.setTitle(getResources().getString(R.string.buttonmakefriendrequested))
			.setMessage(getResources().getString(R.string.messagemakefriendrequested)+" "+userReveive.getNickName())
			.setPositiveButton(getResources().getString(R.string.ok),
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int which) {
							int i = arrs.indexOf(userReveive);
							arrs.get(i).setIsAccpet("null");
							adapSearch.setArrayUser(arrs);
							adapSearch.notifyDataSetChanged();
							isExpanded = false;
							
							// Collapse
							new CollapseAnimation(slidingPanel, panelWidth,
									TranslateAnimation.RELATIVE_TO_SELF, 0.75f,
									TranslateAnimation.RELATIVE_TO_SELF, 0.0f, 0, 0.0f,
									0, 0.0f);
						}
			}).show();
			
			super.onPostExecute(result);
		}
	}
	
	//result set 
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode==Configuration.requestMakeFriendToLogin && resultCode==RESULT_OK){//register then login
			u = new Database(SearchFriend.this).getUser();
			adapSearch.setUserRoot(u.getUserId());
			if(!u.getUserId().equalsIgnoreCase(userReveive.getUserId())){
				new AsynSendRequest().execute(new String[]{u.getUserId(),userReveive.getUserId(),userReveive.getNickName()});
			}
			for (FamiliarFriend f : arrs) {
				if(f.getUserId().equalsIgnoreCase(u.getUserId())){
					arrs.remove(f);
				}
			}
			adapSearch.setArrayUser(arrs);
			adapSearch.notifyDataSetChanged();
		
		}
	}
	//class get gesture
	public class ActivitySwipeDetector implements View.OnTouchListener {

		static final String logTag = "ActivitySwipeDetector";
		static final int MIN_DISTANCE = 200;
		private float downX, downY, upX, upY;

		public void onRightToLeftSwipe() {
			Log.d("item", "RightToLeftSwipe!");
			if (isExpanded) {
				isExpanded = false;

				// Collapse
				new CollapseAnimation(slidingPanel, panelWidth,
						TranslateAnimation.RELATIVE_TO_SELF, 0.75f,
						TranslateAnimation.RELATIVE_TO_SELF, 0.0f, 0, 0.0f, 0,
						0.0f);
			}
		}

		public void onLeftToRightSwipe() {
			Log.d("item", "LeftToRightSwipe!");
			if (!isExpanded) {
				isExpanded = true;

				// Expand
				new ExpandAnimation(slidingPanel, panelWidth,
						Animation.RELATIVE_TO_SELF, 0.0f,
						Animation.RELATIVE_TO_SELF, 0.75f, 0, 0.0f, 0, 0.0f);
			}
		}

		public void onTopToBottomSwipe() {
			Log.d("logTag", "onTopToBottomSwipe!");
			// activity.doSomething();
		}

		public void onBottomToTopSwipe() {
			Log.d("logTag", "onBottomToTopSwipe!");
			// activity.doSomething();
		}

		public boolean onTouch(View v, MotionEvent event) {
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN: {
				downX = event.getX();
				downY = event.getY();
				return true;
			}
			case MotionEvent.ACTION_UP: {
				upX = event.getX();
				upY = event.getY();

				float deltaX = downX - upX;
				float deltaY = downY - upY;
				Log.d("item", deltaX + ":" + downX + "-" + upX);
				// swipe horizontal?
				if (Math.abs(deltaX) > MIN_DISTANCE) {
					// left or right
					Log.d("item", deltaX + "âa");
					if (deltaX < 0) {
						this.onLeftToRightSwipe();
						return true;
					}
					if (deltaX > 0) {
						this.onRightToLeftSwipe();
						return true;
					}
				} else {
					Log.i(logTag, "Swipe was only " + Math.abs(deltaX)
							+ " long, need at least " + MIN_DISTANCE);
					return false; // We don't consume the event
				}

				return true;
			}
			}
			return false;
		}

	}
	public void updateNotify(ArrayList<FamiliarFriend> arr){
		this.arrs = arr;
		adapSearch.notifyDataSetChanged();
	}
	//sent request make friend
	class AsynSendRequest extends AsyncTask<String, Void, String>
	{
		ProgressDialog progress;
		
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			progress = ProgressDialog.show(SearchFriend.this, "",
					getResources().getString(R.string.titledialogsendmakefriend), true);
		}
		@Override
		protected String doInBackground(String... params) {
			return new JsonParse().sendRequestMakeFriend(SearchFriend.this, params, userReveive.getUserId());
		}
		@Override
		protected void onPostExecute(String result) {
			try {
				JSONObject json = new JSONObject(result);
				String r = json.getString(Configuration.result);
				if(r.equalsIgnoreCase("-2")){
					Utility.ShowAlert(SearchFriend.this, getResources().getString(R.string.titledialogmakefriend), getResources().getString(R.string.messagemakefriendexist));
				}
				else if(r.equalsIgnoreCase("-1")){
					Utility.ShowAlert(SearchFriend.this, getResources().getString(R.string.titledialogmakefriend), getResources().getString(R.string.messagemakefriendsent) +" " +userReveive.getNickName());
					int index = arrs.indexOf(userReveive);
					arrs.get(index).setIsAccpet(0+"");
					adapSearch.setArrayUser(arrs);
					adapSearch.notifyDataSetChanged();
				}
				else{
					//Utility.ShowAlert(SearchFriend.this, getResources().getString(R.string.titledialogmakefriend), getResources().getString(R.string.messagemakefriendsuccess));
					new AlertDialog.Builder(SearchFriend.this)
					.setTitle(getResources().getString(R.string.titledialogmakefriend))
					.setMessage(getResources().getString(R.string.messagemakefriendsuccess))
					.setPositiveButton(getResources().getString(R.string.ok),
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									int index = arrs.indexOf(userReveive);
									arrs.get(index).setIsAccpet(0+"");
									adapSearch.setArrayUser(arrs);
									adapSearch.notifyDataSetChanged();
									if(isExpanded){
										isExpanded = false;
										// Collapse
										new CollapseAnimation(slidingPanel, panelWidth,
												TranslateAnimation.RELATIVE_TO_SELF, 0.75f,
												TranslateAnimation.RELATIVE_TO_SELF, 0.0f, 0, 0.0f,
												0, 0.0f);
									}
								}
					}).show();
					
				}
				
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			progress.dismiss();			
			super.onPostExecute(result);
		}
	}
	
	//class load more search friend
	public class EndlessScrollListener implements OnScrollListener {

		private int visibleThreshold;
		private int previousTotal;
		private boolean loading;

		public EndlessScrollListener() {
			visibleThreshold = 1;
			previousTotal = 0;
			loading = true;
		}

		public void onScroll(AbsListView view, int firstVisibleItem,
				int visibleItemCount, int totalItemCount) {
			if (loading) {
				if (totalItemCount > previousTotal) {
					loading = false;
					previousTotal = totalItemCount;			
				}
			}
			if (!loading
					&& (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
				String userid;
				if(u==null){
					userid = "0";
				}
				else{
					userid = u.getUserId();
				}
				new AsynMoreSearchFriend().execute(new String[]{keyWord,userid});
				loading = true;
			}			
		}

		public void onScrollStateChanged(AbsListView view, int scrollState) {
			// TODO Auto-generated method stub

		}

	}
	//load search friend by key
	class AsynSearchFriend extends AsyncTask<String, Void, String>
	{
		ProgressDialog progress;
		String userid;
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			arrs.clear();
			pageLV = 0;
			progress = ProgressDialog.show(SearchFriend.this, "",
					getResources().getString(R.string.titlesearchfriend), true);
			v.setVisibility(View.VISIBLE);
		}
		@Override
		protected String doInBackground(String... params) {
			userid = params[1];
			return new JsonParse().getSearchFriend(SearchFriend.this, params[0],params[1], ++pageLV);
		}
		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			try {
				JSONObject json = new JSONObject(result);
				String res = json.getString(Configuration.result);
				Log.d("log",res);
				if(!res.equalsIgnoreCase(Configuration.norow)){
					JSONArray arr = json.getJSONArray(Configuration.result);
					for(int i=0;i<arr.length();i++){
						JSONObject obj = arr.getJSONObject(i);
						String userid = obj.getString(FamiliarFriend.keyUserId);
						String email = obj.getString(FamiliarFriend.keyGmail);
						String nickname = obj.getString(FamiliarFriend.keyNickName);
						String birthday = obj.getString(FamiliarFriend.keyBirthday);
						String link = obj.getString(FamiliarFriend.keyLink);
						String isAccept = obj.getString(FamiliarFriend.keyIsAccept);
						String gender = obj.getString(FamiliarFriend.keygender);
						
						FamiliarFriend f = new FamiliarFriend(userid, nickname, birthday, link, email, isAccept, gender);
						arrs.add(f);
					}
					adapSearch = new AdapterSearchFriend(SearchFriend.this, arrs,userid).new Adapter();
					lvSearch.setAdapter(adapSearch);
					v.setVisibility(View.GONE);
					tvNoSearch.setVisibility(View.GONE);
					lvSearch.setVisibility(View.VISIBLE);
					Log.d("log",arrs.size()+"");
				}
				else{
					tvNoSearch.setVisibility(View.VISIBLE);
					lvSearch.setVisibility(View.GONE);
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			progress.dismiss();
		}
	}
	
	//update infor sliding menu
	public void updateInforSldingMenu(FamiliarFriend f){
		tvBirthday.setText(f.getBirthday());
		tvEmail.setText(f.getGmail());
		tvGender.setText((f.getGender().equalsIgnoreCase("1")?getResources().getString(R.string.hintmaleregister):getResources().getString(R.string.hintfemaleregister)));
		tvNickname.setText(f.getNickName());
		if(f.getIsAccpet().equalsIgnoreCase("2")){
			btnMake.setVisibility(View.GONE);
		}
		else if(f.getIsAccpet().equalsIgnoreCase("0")){
			btnMake.setVisibility(View.VISIBLE);
			btnMake.setText(getResources().getString(R.string.buttonmakefriendrequested));
		}
		else if(f.getIsAccpet().equalsIgnoreCase("null")||f.getIsAccpet().equalsIgnoreCase("1")){
			btnMake.setVisibility(View.VISIBLE);
			btnMake.setText(getResources().getString(R.string.buttonmakefriend));
		}
		
		AQuery aq = new AQuery(SearchFriend.this);
		Bitmap placeholder = aq.getCachedImage(R.drawable.imageload);
		aq.id(R.id.imageViewAvatarSlidingMenuMakeFriend).image(f.getLink(), true,
					true, 0, 0, placeholder, AQuery.FADE_IN_NETWORK, 1.0f);
		userReveive = f;
	}
	
	//load more search friend
	class AsynMoreSearchFriend extends AsyncTask<String, Void, String>
	{
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			v.setVisibility(View.VISIBLE);
		}
		@Override
		protected String doInBackground(String... params) {
			return new JsonParse().getSearchFriend(SearchFriend.this, params[0],params[1], ++pageLV);
		}
		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			try {
				JSONObject json = new JSONObject(result);
				String res = json.getString(Configuration.result);
				if(!res.equalsIgnoreCase(Configuration.norow)){
					JSONArray arr = json.getJSONArray(Configuration.result);
					for(int i=0;i<arr.length();i++){
						JSONObject obj = arr.getJSONObject(i);
						String userid = obj.getString(FamiliarFriend.keyUserId);
						String email = obj.getString(FamiliarFriend.keyGmail);
						String nickname = obj.getString(FamiliarFriend.keyNickName);
						String birthday = obj.getString(FamiliarFriend.keyBirthday);
						String link = obj.getString(FamiliarFriend.keyLink);
						String gender = obj.getString(FamiliarFriend.keygender);
						String isAccept = obj.getString(FamiliarFriend.keyIsAccept);
						
						FamiliarFriend f = new FamiliarFriend(userid, nickname, birthday, link, email, isAccept,gender);
						arrs.add(f);
					}
					adapSearch.notifyDataSetChanged();					
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			v.setVisibility(View.GONE);
			lvSearch.setVisibility(View.VISIBLE);
		}
	}
	//load falimilar friend
	class AsynLoadResemble extends AsyncTask<Void, Void, String>
	{
		String userid; 
		ArrayList<FamiliarFriend> arrs = new ArrayList<FamiliarFriend>();
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			progressGal.setVisibility(View.VISIBLE);
			u = new Database(SearchFriend.this).getUser();
			if(u!=null){
				userid = u.getUserId();
			}
			else{
				userid = "0";
			}
		}
		@Override
		protected String doInBackground(Void... params) {
			return new JsonParse().getResembleFriend(SearchFriend.this, userid+"", ++pageGal);
		}
		
		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			JSONObject json;
			try {
				json = new JSONObject(result);
				String res = json.getString(Configuration.result);
				if(!res.equalsIgnoreCase(Configuration.norow)){
					JSONArray arr = json.getJSONArray(Configuration.result);
					for(int i=0;i<arr.length();i++){
						JSONObject obj = arr.getJSONObject(i);
						String userid = obj.getString(FamiliarFriend.keyUserId);
						String email = obj.getString(FamiliarFriend.keyGmail);
						String nickname = obj.getString(FamiliarFriend.keyNickName);
						String birthday = obj.getString(FamiliarFriend.keyBirthday);
						String link = obj.getString(FamiliarFriend.keyLink);
						
						FamiliarFriend f = new FamiliarFriend(userid, nickname, birthday, link, email);
						arrs.add(f);
					}
					adapFamiliar = new AdapterFamiliarFriend(SearchFriend.this, arrs).new Adapter();
					galSearch.setAdapter(adapFamiliar);
					
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			galSearch.setVisibility(View.VISIBLE);
			progressGal.setVisibility(View.GONE);
		}
	}
}
